package miniapp

import (
	"github.com/medivhzhan/weapp/v2"
	"hr/config"
)

func GetPhone(code, str, iv string) (string, error) {
	res, err := weapp.Login(config.AppConfig.Miniapp.Appid, config.AppConfig.Miniapp.Secret, code)
	if err != nil {
		return "", err
	}

	if err := res.GetResponseError(); err !=nil {
		return "", err
	}
	rea, err := weapp.DecryptMobile(res.SessionKey, str, iv )
	if err != nil {
		return "", err
	}
	return rea.PhoneNumber, nil
}

func GetUserInfo(code, raw, ed, sign, iv string) (*weapp.UserInfo, error) {
	res, err := weapp.Login(config.AppConfig.Miniapp.Appid, config.AppConfig.Miniapp.Secret, code)
	if err != nil {
		return &weapp.UserInfo{}, err
	}

	if err := res.GetResponseError(); err !=nil {
		return &weapp.UserInfo{}, err
	}
	rea, err := weapp.DecryptUserInfo( res.SessionKey, raw, ed, sign, iv)
	if err != nil {
		return &weapp.UserInfo{}, err
	}
	return rea, nil
}